package com.local.admin.controller;

import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.user.feign.UserServiceFeign;
import com.local.user.pojo.UserListRequestParam;
import com.local.user.pojo.UserLockRequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Controller
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UserServiceFeign userServiceFeign;

    /**
     * 用户管理列表页面
     * @param request
     * @return
     */
    @GetMapping
    public String index(HttpServletRequest request) {
        request.setAttribute("path", "users");

        return "admin/user";
    }


    /**
     * user列表数据
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param userName
     * @param lockedFlag
     * @param isDeleted
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                           @RequestParam("sidx") String sort, @RequestParam("order") String order,
                           @RequestParam(name = "userName", required = false) String userName,
                           @RequestParam(name = "lockedFlag", required = false) Integer lockedFlag,
                           @RequestParam(name = "isDeleted", required = false) Integer isDeleted) {
        page = Objects.isNull(page) || page <= 0 ? 1 : page;
        limit = Objects.isNull(limit) || page <= 0 ? 10 : limit;
        sort = Objects.isNull(sort) || sort.isEmpty() ? "user_id" : sort;
        order = Objects.isNull(order) || order.isEmpty() ? "desc" : order;
        UserListRequestParam requestParam = new UserListRequestParam(page, limit, sort, order, userName, lockedFlag, isDeleted);
        ResultUtil listResult = userServiceFeign.list(requestParam);
        return listResult;
    }


    /**
     * 锁定/解锁用户状态
     * @param lockedFlag
     * @param ids
     * @return
     */
    @PostMapping("/lock/{lockedFlag}")
    @ResponseBody
    public ResultUtil lock(@PathVariable("lockedFlag") Integer lockedFlag, @RequestBody Integer[] ids) {
        UserLockRequestParam requestParam = new UserLockRequestParam(ids, lockedFlag);
        ResultUtil lockResult = userServiceFeign.lock(requestParam);
        return lockResult;
    }
}
